<template>
	<view class="container">
		<!-- 页面导航栏 -->
		<page-navbar title="地址管理"></page-navbar>

		<!-- 地址列表 -->
		<view class="address-list">
			<view class="address-item" v-for="address in addressList" :key="address.id" @click="selectAddress(address)">
				<!-- 地址信息 -->
				<view class="address-content">
					<view class="address-header">
						<text class="recipient-name">{{ address.name }}</text>
						<text class="recipient-phone">{{ address.phone }}</text>
						<view class="default-tag" v-if="address.is_default == 2">默认</view>
					</view>
					<text class="address-detail">{{
						address.province + address.city + address.district + address.address }}</text>
				</view>

				<!-- 选择状态 -->
				<view class="select-status">
					<view class="radio-btn" :class="{ checked: currentAddress.id === address.id }">
						<view class="radio-inner" v-if="currentAddress.id === address.id"></view>
					</view>
				</view>

				<!-- 操作按钮 -->
				<view class="address-actions">
					<view class="action-btn edit-btn" @click.stop="editAddress(address)">
						<uni-icons type="compose" size="16" color="#666"></uni-icons>
						<text>编辑</text>
					</view>
					<view class="action-btn delete-btn" @click.stop="deleteAddress(address)"
						v-if="address.is_default != 2">
						<uni-icons type="trash" size="16" color="#ff6b6b"></uni-icons>
						<text>删除</text>
					</view>
					<view class="action-btn default-btn" @click.stop="setDefault(address)"
						v-if="address.is_default != 2">
						<uni-icons type="star" size="16" color="#ffb400"></uni-icons>
						<text>设为默认</text>
					</view>
				</view>
			</view>

			<!-- 空状态 -->
			<view class="empty-state" v-if="addressList.length === 0">
				<uni-icons type="location" size="60" color="#ccc"></uni-icons>
				<text class="empty-text">暂无收货地址</text>
				<text class="empty-hint">点击下方按钮添加新地址</text>
			</view>
		</view>

		<!-- 添加地址按钮 -->
		<view class="add-address-btn" @click="addAddress">
			<uni-icons type="plus" size="20" color="white"></uni-icons>
			<text class="btn-text">添加新地址</text>
		</view>

		<!-- 底部确认按钮 -->
		<view class="bottom-bar" v-if="showConfirmButton">
			<view class="confirm-btn" @click="confirmSelection">
				<text class="confirm-text">确认选择</text>
			</view>
		</view>
	</view>
</template>

<script setup>
import { ref, reactive, onMounted, computed, toRefs } from 'vue';
import { onLoad, onShow } from '@dcloudio/uni-app';
import PageNavbar from '@/components/page-navbar/page-navbar.vue';
import api from "@/api"

import useSelectStore from '@/stores/select';
const selectStore = useSelectStore();
const { selectedAddress } = toRefs(selectStore);

// 弹框引用
const addressPopup = ref(null);

// 页面参数
const pageParams = ref({});
// 页面是否为选择
const showConfirmButton = ref(false);
// 当前选中的地址
const currentAddress = ref({});

// 地址列表
const addressList = ref([]);

// 页面加载
onLoad((options) => {
	pageParams.value = options || {};
	currentAddress.value = { ...selectedAddress.value }
});

onShow(() => {
	getList()
})

const getList = () => {
	api.commerceApi.addressList({ loding: true }).then(res => {
		console.log("地址列表", res);
		addressList.value = res.list || [];
		// 如果是从购买页面跳转过来的，显示确认按钮
		if (pageParams.value.from === 'purchase') {
			showConfirmButton.value = true;
		}
	})
}

// 选择地址
const selectAddress = (address) => {
	currentAddress.value = { ...address }
};

// 添加地址
const addAddress = () => {
	uni.navigateTo({
		url: '/pages/commerce/deitAddress'
	})
};

// 编辑地址
const editAddress = (address) => {
	uni.navigateTo({
		url: '/pages/commerce/deitAddress?id=' + address.id
	})
};

// 删除地址
const deleteAddress = (address) => {
	uni.showModal({
		title: '确认删除',
		content: '确定要删除这个地址吗？',
		success: (res) => {
			if (res.confirm) {
				const index = addressList.value.findIndex(item => item.id === address.id);
				if (index > -1) {
					addressList.value.splice(index, 1);
					uni.showToast({
						title: '删除成功',
						icon: 'success'
					});
				}
			}
		}
	});
};

// 设为默认
const setDefault = (address) => {
	let obj = { ...address }
	obj.is_default = 2
	api.commerceApi.setupAddress(obj).then(res => {
		uni.showToast({
			title: '设置成功',
			icon: 'success'
		});
		getList()
	})
};

// 确认选择
const confirmSelection = () => {
	let result = addressList.value.find(item => item.id === currentAddress.value.id)

	if (!result) {
		uni.showToast({
			title: '请选择一个地址',
			icon: 'none'
		});
		return;
	}
	selectedAddress.value = { ...currentAddress.value }
	uni.navigateBack();
};

</script>

<style lang="scss" scoped>
.container {
	background-color: #f2f1f6;
	min-height: 100vh;
	width: 100vw;
	padding-bottom: 120rpx;
}

.address-list {
	padding: 20rpx;

	.address-item {
		background: white;
		border-radius: 16rpx;
		padding: 30rpx;
		margin-bottom: 20rpx;
		position: relative;

		.address-content {
			flex: 1;
			margin-right: 60rpx;

			.address-header {
				display: flex;
				align-items: center;
				gap: 20rpx;
				margin-bottom: 15rpx;

				.recipient-name {
					font-size: 28rpx;
					font-weight: bold;
					color: #333;
				}

				.recipient-phone {
					font-size: 26rpx;
					color: #666;
				}

				.default-tag {
					background: #56ab2f;
					color: white;
					font-size: 20rpx;
					padding: 4rpx 12rpx;
					border-radius: 8rpx;
				}
			}

			.address-detail {
				font-size: 26rpx;
				color: #666;
				line-height: 1.4;
			}
		}

		.select-status {
			position: absolute;
			top: 30rpx;
			right: 30rpx;

			.radio-btn {
				width: 32rpx;
				height: 32rpx;
				border: 2rpx solid #ddd;
				border-radius: 50%;
				display: flex;
				align-items: center;
				justify-content: center;

				&.checked {
					border-color: #56ab2f;

					.radio-inner {
						width: 16rpx;
						height: 16rpx;
						background: #56ab2f;
						border-radius: 50%;
					}
				}
			}
		}

		.address-actions {
			display: flex;
			align-items: center;
			gap: 30rpx;
			margin-top: 25rpx;
			padding-top: 25rpx;
			border-top: 1rpx solid #f0f0f0;

			.action-btn {
				display: flex;
				align-items: center;
				gap: 8rpx;
				padding: 12rpx 20rpx;
				border-radius: 8rpx;
				font-size: 24rpx;

				&.edit-btn {
					background: rgba(102, 102, 102, 0.1);
					color: #666;
				}

				&.delete-btn {
					background: rgba(255, 107, 107, 0.1);
					color: #ff6b6b;
				}

				&.default-btn {
					background: rgba(255, 180, 0, 0.1);
					color: #ffb400;
				}
			}
		}
	}

	.empty-state {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		padding: 120rpx 0;

		.empty-text {
			font-size: 28rpx;
			color: #999;
			margin: 20rpx 0 10rpx;
		}

		.empty-hint {
			font-size: 24rpx;
			color: #ccc;
		}
	}
}

.add-address-btn {
	margin: 20rpx 48rpx;
	background: linear-gradient(135deg, #56ab2f, #a8e6cf);
	color: white;
	padding: 20rpx 24rpx;
	border-radius: 50rpx;
	display: flex;
	align-items: center;
	justify-content: center;
	gap: 15rpx;
	box-shadow: 0 8rpx 24rpx rgba(86, 171, 47, 0.4);

	.btn-text {
		font-size: 28rpx;
		font-weight: bold;
	}
}

.bottom-bar {
	position: fixed;
	bottom: 0;
	left: 0;
	right: 0;
	background: white;
	padding: 20rpx 30rpx;
	box-shadow: 0 -4rpx 20rpx rgba(0, 0, 0, 0.1);
	z-index: 999;

	.confirm-btn {
		background: linear-gradient(135deg, #56ab2f, #a8e6cf);
		color: white;
		padding: 25rpx;
		border-radius: 50rpx;
		text-align: center;
		box-shadow: 0 4rpx 16rpx rgba(86, 171, 47, 0.4);

		.confirm-text {
			font-size: 28rpx;
			font-weight: bold;
		}
	}
}

.address-form {
	background: white;
	border-radius: 16rpx;
	width: 80vw;
	max-height: 80vh;
	overflow: hidden;
	box-sizing: border-box;

	.form-header {
		display: flex;
		align-items: center;
		justify-content: space-between;
		padding: 30rpx;
		border-bottom: 1rpx solid #f0f0f0;
		box-sizing: border-box;

		.form-title {
			font-size: 32rpx;
			font-weight: bold;
			color: #333;
		}
	}

	.form-content {
		padding: 30rpx;
		max-height: 60vh;
		overflow-y: auto;

		.form-item {
			margin-bottom: 30rpx;
			display: flex;
			align-items: center;
			justify-content: space-between;
			gap: 20rpx;

			.form-label {
				display: block;
				font-size: 26rpx;
				color: #333;
				margin-bottom: 15rpx;
			}

			.form-input {
				width: 100%;
				padding: 20rpx;
				border: 2rpx solid #e0e0e0;
				border-radius: 8rpx;
				font-size: 26rpx;
				box-sizing: border-box;
				height: 80rpx;

				&:focus {
					border-color: #56ab2f;
				}
			}

			.form-textarea {
				width: 100%;
				padding: 20rpx;
				border: 2rpx solid #e0e0e0;
				border-radius: 8rpx;
				font-size: 26rpx;
				min-height: 120rpx;
				box-sizing: border-box;

				&:focus {
					border-color: #56ab2f;
				}
			}

			.region-picker {
				display: flex;
				align-items: center;
				justify-content: space-between;
				padding: 20rpx;
				border: 2rpx solid #e0e0e0;
				border-radius: 8rpx;

				.region-text {
					font-size: 26rpx;
					color: #333;

					&.placeholder {
						color: #999;
					}
				}
			}

			.switch-item {
				display: flex;
				align-items: center;
				justify-content: space-between;

				.switch-label {
					font-size: 26rpx;
					color: #333;
				}
			}
		}
	}

	.form-actions {
		display: flex;
		border-top: 1rpx solid #f0f0f0;

		.cancel-btn,
		.save-btn {
			flex: 1;
			padding: 30rpx;
			text-align: center;
			font-size: 28rpx;
		}

		.cancel-btn {
			color: #666;
			border-right: 1rpx solid #f0f0f0;
		}

		.save-btn {
			color: #56ab2f;
			font-weight: bold;
		}
	}
}
</style>
